package sd.czw.com.mmgapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.snappingstepper.SnappingStepper;
import com.bumptech.glide.Glide;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.shopcart.ShopCartModel;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/21.
 */
public class ShopCartAdapter extends SwipeMenuAdapter<ShopCartAdapter.ViewHolder> {

    private Context context;
    private List<ShopCartModel.ErrmsgBean.GoodsListBean> datas;

    private CzwOnCheckedClickListener czwOnCheckedClickListener;
    private CzwOnNumsClickListener czwOnNumsClickListener;
    private String rec_type;
    private List<ShopCartModel.ErrmsgBean.TotalBean> totalBean;
    private List<Integer> checkPositionlist;
    private List<ShopCartModel.ErrmsgBean.GoodsListBean> checkeddata = new ArrayList<>();// 选中的数据



    /**
     * checkbox接口
     */
    public interface CzwOnCheckedClickListener{
        void onCheckedListener(int position, int value);
    }

    public void setCzwOnCheckedClickListener(CzwOnCheckedClickListener czwOnCheckedClickListener) {
        this.czwOnCheckedClickListener = czwOnCheckedClickListener;
    }


    /**
     *数量改变接口
     */
    public interface CzwOnNumsClickListener{
        void onNumsClickListener(SnappingStepper stepper,int position);
    }

    public void setCzwOnNumsClickListener(CzwOnNumsClickListener czwOnNumsClickListener) {
        this.czwOnNumsClickListener = czwOnNumsClickListener;
    }

    public ShopCartAdapter(Context context, List<ShopCartModel.ErrmsgBean.GoodsListBean> datas) {
        this.context = context;
        this.datas = datas;
        //this.isAllcheck = isAllCheck;
        checkPositionlist = new ArrayList<>();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(context).inflate(R.layout.shopcart_rv, parent, false);
    }

    @Override
    public ViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new ViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //holder.isRecyclable();
        holder.checkGoods.setTag(new Integer(position));
        if (datas.get(position).getRec_type().equals("0")){
            holder.checkGoods.setChecked(true);
            //checkeddata.add(datas.get(position));

        }else {
            holder.checkGoods.setChecked(false);
            //checkeddata.remove(datas.get(position));
        }
        if (checkPositionlist != null){
            holder.checkGoods.setChecked((checkPositionlist.contains(new Integer(position)) ? true:false));
        }else {
            holder.checkGoods.setChecked(false);
        }

        Glide.with(context).load(Contants.BASE_URL + datas.get(position).getGoods_thumb()).asBitmap().into(holder.ivGoods);
        holder.tvGoodsTitle.setText(datas.get(position).getGoods_name());
        holder.tvGoodsPrice.setText(datas.get(position).getGoods_price());
        holder.tvMake.setText("供应商:" + datas.get(position).getSuppliers_name());
        holder.stepper.setValue(datas.get(position).getGoods_number());
//        holder.stepper.setOnValueChangeListener(new SnappingStepperValueChangeListener() {
//            @Override
//            public void onValueChange(View view, final int value) {
//                czwOnNumsClickListener.onNumsClickListener(holder.stepper,position);
//                }
//        });

        /**
         * checkbox选择事件
         */
        holder.checkGoods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                //ShopCartModel.ErrmsgBean.GoodsListBean goodsListBean = datas.get(position);
                holder.checkGoods.getTag();
                if (isChecked == true){
                   datas.get(position).setRec_type("0");
                    if (!checkPositionlist.contains(holder.checkGoods.getTag())){
                        //checkeddata.add(goodsListBean);
                        checkPositionlist.add(new Integer(position));
                    }
                }else {
                    datas.get(position).setRec_type("1");
                    if (checkPositionlist.contains(holder.checkGoods.getTag())){
                       // checkeddata.remove(goodsListBean);
                        checkPositionlist.remove(new Integer(position));
                    }
                }

                if (czwOnCheckedClickListener != null){
                    czwOnCheckedClickListener.onCheckedListener(position,holder.stepper.getValue());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.check_goods)
        CheckBox checkGoods;
        @Bind(R.id.iv_goods)
        ImageView ivGoods;
        @Bind(R.id.tv_goods_title)
        TextView tvGoodsTitle;
        @Bind(R.id.tv_make)
        TextView tvMake;
        @Bind(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @Bind(R.id.stepper)
        SnappingStepper stepper;

        ViewHolder(View itemview) {
            super(itemview);
            ButterKnife.bind(this, itemview);
        }
    }
}
